<template>
  <el-drawer title="CDK列表" :visible="visible" :size="'calc(100vw - 256px)'" :lock-scroll="false"
    @update:visible="updateVisible" :close-on-click-modal="false">
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
          @submit.native.prevent>
          <el-row :gutter="15">
            <el-col :lg="6" :md="12">
              <div class="ele-form-actions"></div>
            </el-col>
          </el-row>
        </el-form>
        <!-- 数据表格 -->
        <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
          height="calc(100vh - 315px)" :initLoad = "false">
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button size="small" type="primary" icon="el-icon-upload2"
              @click="dialogVisible = true">导入礼包CDK</el-button>
            <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatchCdk"
              v-if="permission.includes('sys:activityexchange:dall')">批量删除
            </el-button>
            <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatchCdk(1)"
              v-if="permission.includes('sys:activityexchange:dall')">删除所有未领取CDK
            </el-button>
          </template>
          <template slot="is_get" slot-scope="{ row }">
            <el-tag v-if="row.is_get === 0" type="" size="small">未领取</el-tag>
            <el-tag v-if="row.is_get === 1" type="success" size="small">已领取</el-tag>
          </template>
        </ele-pro-table>
      </el-card>
      <el-dialog title="导入模块" :visible.sync="dialogVisible" width="50%" :append-to-body="true">

        <el-upload drag action="" class="ele-block" v-loading="loading" accept=".xls,.xlsx" :show-file-list="false"
          :before-upload="beforeUpload">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或 <em>点击上传</em>
          </div>
          <div class="el-upload__tip ele-text-center" slot="tip">
            <span>只能上传xls、xlsx文件，</span>
            <el-link download @click="exportExcel" type="primary" :underline="false"
              style="vertical-align: baseline;">下载模板
            </el-link>
          </div>
        </el-upload>
      </el-dialog>
    </div>
  </el-drawer>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "taskCdk",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    // taskId: Number
  },
  // components: { ActivityExchangeCdkEdit },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/BoxGametaskConfig/getTaskCdkList",
      dialogVisible: false,
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },
        {
          prop: "id",
          label: "ID",
          width: 60,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "gametask_id",
          label: "活动编号",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "cdk",
          label: "cdk",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "is_get",
          label: "是否被领取",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          slot: "is_get"
        },
        {
          prop: "user_id",
          label: "领取人编号",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "get_time",
          label: "领取时间",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      loading: false,
      taskId: 0,
      deleteWarning:""
    };
  },

  methods: {
    // async getList() {
    //   let result = await this.$http.post(this.url, this.where)
    //   this.resultList = result.data.data;
    // },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    // openEdit(row) {
    //   this.current = row;
    //   this.showEdit = true;
    // },
    /* 导出数据Excel */
    exportExcel() {
      this.$http.post("/activityexchange/export").then(res => {
        let data = res.data;
        if (data.code == 0) {
          // 下载文件
          window.location.href = data.data;
          this.$message({
            type: "success",
            message: "导出成功"
          });
        } else {
          this.$message.error("导出失败");
        }
      });
    },
    beforeUpload(file) {
      this.loading = true;
      let formData = new FormData();
      formData.append("exchange_file", file);
      formData.append("task_id", this.taskId);
      this.$http
        .post("/BoxGametaskConfig/import", formData)
        .then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
            this.dialogVisible = false;
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
      return false;
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    open() {
      this.where.task_id = this.taskId;
      this.reload();
    },
    /* 批量删除 */
    removeBatchCdk(value) {
      if(value != 1){
        if (!this.selection.length) {
          this.$message.error("请至少选择一条数据");
          return;
        }
        this.deleteWarning = "确定要删除选中的CDK吗";
      }else{
        this.deleteWarning = "确定要删除该任务所有未领取的CDK吗";
      }
      
      this.$confirm(this.deleteWarning, "警告", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/BoxGametaskConfig/delete", {
              id: this.selection.map((d) => d.id),
              task_id: this.taskId,
              is_all: value
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },
  }
};
</script>

<style scoped></style>
